package com.example.dao;

import com.example.entity.Shop;
import org.springframework.stereotype.Repository;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

@Repository
public class ShopDao {

    private Connection getConnection() throws SQLException {
        return DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/elm",
                "root",
                "123456"
        );
    }

    public List<Shop> getAll(String searchName) {
        List<Shop> shops = new ArrayList<>();
        String sql = "SELECT * FROM shop WHERE shopName LIKE ?";
        try (Connection conn = getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, "%" + searchName + "%");
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                Shop shop = new Shop();
                shop.setShopId(rs.getInt("shopId"));
                shop.setShopName(rs.getString("shopName"));
                shop.setShopAddress(rs.getString("shopAddress"));
                shop.setShopExplain(rs.getString("shopExplain"));
                shop.setShopImg(rs.getString("shopImg"));
                shop.setShopType(rs.getString("shopType"));
                shop.setStartPrice(rs.getDouble("startPrice"));
                shop.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                shop.setRemarks(rs.getString("remarks"));
                shops.add(shop);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return shops;
    }

    public Shop getShopById(Integer shopId) {
        String sql = "SELECT * FROM shop WHERE shopId = ?";
        try (Connection conn = getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, shopId);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                Shop shop = new Shop();
                shop.setShopId(rs.getInt("shopId"));
                shop.setShopName(rs.getString("shopName"));
                shop.setShopAddress(rs.getString("shopAddress"));
                shop.setShopExplain(rs.getString("shopExplain"));
                shop.setShopImg(rs.getString("shopImg"));
                shop.setShopType(rs.getString("shopType"));
                shop.setStartPrice(rs.getDouble("startPrice"));
                shop.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                shop.setRemarks(rs.getString("remarks"));
                return shop;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<Shop> listShopByShopType(String shopType) {
        List<Shop> shops = new ArrayList<>();
        String sql = "SELECT * FROM shop WHERE shopType = ?";
        try (Connection conn = getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, shopType);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                Shop shop = new Shop();
                shop.setShopId(rs.getInt("shopId"));
                shop.setShopName(rs.getString("shopName"));
                shop.setShopAddress(rs.getString("shopAddress"));
                shop.setShopExplain(rs.getString("shopExplain"));
                shop.setShopImg(rs.getString("shopImg"));
                shop.setShopType(rs.getString("shopType"));
                shop.setStartPrice(rs.getDouble("startPrice"));
                shop.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                shop.setRemarks(rs.getString("remarks"));
                shops.add(shop);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return shops;
    }
}